package com.hg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hg.mapper.HgHistoryOrderMapper;
import com.hg.pojo.HgMallDish;
import com.hg.pojo.HgMallOrderChilder;
import com.hg.pojo.HgMallOrders;
import com.hg.pojo.HgMallShop;
import com.hg.pojo.dto.SelectOrderDishVo;
import com.hg.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class HgHistoryOrderServiceImpl extends ServiceImpl<HgHistoryOrderMapper, HgMallOrders> implements HgHistoryOrderService {

    @Autowired
    private HgMallOrderChilderService hgMallOrderChilderService;

    @Autowired
    private HgMallDishService hgMallDishService;

    @Autowired
    private HgMallShopService hgMallShopService;

    @Autowired
    private HgUserService hgUserService;

    @Override
    public Page<HgMallOrders> selectHgMallOrdersList(int currentPage, int pageSize,String appid,String orderNum,String oddNum,Long userId) {


        //根据用户id查询出绑定的店铺id
        Long sId = hgUserService.getById(userId).getShopId();
        //如果sId为空则为管理员 直接查询全部
        //开启分页查询
        Page<HgMallOrders> listPage =new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<HgMallOrders> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.select(
                HgMallOrders::getId,
                HgMallOrders::getAppid,
                HgMallOrders::getShopId,
                HgMallOrders::getMealStatus,
                HgMallOrders::getStatus,
                HgMallOrders::getOddNum,
                HgMallOrders::getOrderNum,
                HgMallOrders::getDelFlag);
        queryWrapper.eq(sId != null,HgMallOrders::getShopId,sId);
        queryWrapper.eq(HgMallOrders::getDelFlag,0);
        queryWrapper.eq(HgMallOrders::getStatus,1);
        queryWrapper.orderByDesc(HgMallOrders::getCreateTime);
        queryWrapper.like(appid != null,HgMallOrders::getAppid,appid);
        queryWrapper.like(orderNum != null,HgMallOrders::getOrderNum,orderNum);
        queryWrapper.like(oddNum != null,HgMallOrders::getOddNum,oddNum);
        //执行大订单查询
        Page<HgMallOrders> hgMallOrdersPage = this.page(listPage, queryWrapper);
        //获取本次分页数据
        List<HgMallOrders> BigOrders = hgMallOrdersPage.getRecords();

        //原子操作保证多线程高并发也能计算准确 线程安全的
        AtomicInteger amount = new AtomicInteger(0);

        for (HgMallOrders bigOrder : BigOrders) {
            //提升作用域
            int shopId = 0;
            String shopName = "";

            //获取每一个订单id查询关联的子订单表 然后在子订单表获取 菜品id
            String orderId = bigOrder.getId();
            //执行子订单的查询获取菜品id
            List<Integer> dishIds = getdishIdByOrderId(orderId);
            //遍历此订单所有的菜品
            //创建菜品详情的集合对象
            List<SelectOrderDishVo> selectOrderDishVoList = new ArrayList<>();
            for (Integer dishId : dishIds) {
                //先创建一个菜品详情的对象
                SelectOrderDishVo selectOrderDishVo = new SelectOrderDishVo();

                //查询订单包含几份菜
                LambdaQueryWrapper<HgMallOrderChilder> lmOrderChiler = new LambdaQueryWrapper<>();
                lmOrderChiler.select(
                        HgMallOrderChilder::getId,
                        HgMallOrderChilder::getDishId,
                        HgMallOrderChilder::getOrdersId,
                        HgMallOrderChilder::getDelFlag,
                        HgMallOrderChilder::getDishRequire, //菜品详情
                        HgMallOrderChilder::getDishCount //所需菜品份数
                );
                lmOrderChiler.eq(HgMallOrderChilder::getDishId,dishId).eq(HgMallOrderChilder::getOrdersId,orderId);
                HgMallOrderChilder orderChilder = hgMallOrderChilderService.getOne(lmOrderChiler);

                //根据菜品id查询出这个订单的菜品信息
                LambdaQueryWrapper<HgMallDish> lmDish=new LambdaQueryWrapper<>();
                lmDish.select(
                        HgMallDish::getId,
                        HgMallDish::getName,
                        HgMallDish::getIntroduce,
                        HgMallDish::getPrice,  //菜品的价钱
                        HgMallDish::getType,
                        HgMallDish::getStatus,
                        HgMallDish::getShopId,
                        HgMallDish::getDelFlag,
                        HgMallDish::getSales,
                        HgMallDish::getImg);
                lmDish.eq(HgMallDish::getId,dishId);
                HgMallDish dish = hgMallDishService.getOne(lmDish);
                selectOrderDishVo.setId(dish.getId());
                selectOrderDishVo.setDishName(dish.getName());
                selectOrderDishVo.setDishCount(orderChilder.getDishCount());
                selectOrderDishVo.setIntroduce(dish.getIntroduce());
                selectOrderDishVo.setType(dish.getType());
                shopId = dish.getShopId();
                //根据店铺id查询店铺名称
                LambdaQueryWrapper<HgMallShop> lmShop = new LambdaQueryWrapper<>();
                lmShop.select(HgMallShop::getName);
                lmShop.eq(HgMallShop::getId,shopId);
                shopName = hgMallShopService.getOne(lmShop).getName();

                //往集合中塞数据
                selectOrderDishVoList.add(selectOrderDishVo);

                //计算价格
                amount.addAndGet(dish.getPrice().multiply(new BigDecimal(orderChilder.getDishCount())).intValue());
            }
            bigOrder.setPriceAll(new BigDecimal(amount.get()));
            bigOrder.setShopId(shopId);
            bigOrder.setShopName(shopName);
            //最后把菜品详情集合加入 大功告成
            bigOrder.setOrderDishVo(selectOrderDishVoList);
        }
        //重新给分页数据赋值
        listPage.setRecords(BigOrders);
        return listPage;
    }


    /**
     * 获取菜品id
     * @param orderId
     * @return
     */
    public List<Integer> getdishIdByOrderId(String orderId){
        List<Integer> idList= new ArrayList<>();
        LambdaQueryWrapper<HgMallOrderChilder> lm=new LambdaQueryWrapper<>();
        lm.select(HgMallOrderChilder::getDishId);
        lm.eq(HgMallOrderChilder::getOrdersId,orderId);
        idList=  hgMallOrderChilderService.list(lm).stream().map((item)->{
            return  item.getDishId();
        }).collect(Collectors.toList());
        return idList;
    }
}

